# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    # @param {ListNode} l1
    # @param {ListNode} l2
    # @return {ListNode}
    def mergeTwoLists(self, l1, l2):
        l3 = ListNode(0)
        cur = l3
        while l1 or l2:
            if l1 and l2:
                if l1.val > l2.val:
                    node = ListNode(l2.val)
                    cur.next = node
                    cur = node
                    l2 = l2.next
                else:
                    node = ListNode(l1.val)
                    cur.next = node
                    cur = node
                    l1 = l1.next
            elif l1:
                node = ListNode(l1.val)
                cur.next = node
                cur = node
                l1 = l1.next
            elif l2:
                node = ListNode(l2.val)
                cur.next = node
                cur = node
                l2 = l2.next

        return l3.next 
